package com.qf.shopgoods.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.qf.common.result.ResultEntity;
import com.qf.common.constant.RabbitMqConstants;
import com.qf.entity.Goods;
import com.qf.shopgoods.service.GoodsService;
import org.apache.catalina.LifecycleState;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


@RestController
@RequestMapping("/goodsController")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @RequestMapping("/addGoods")
    public ResultEntity addGoods(@RequestBody Goods goods) {
        boolean insert = goodsService.insert(goods);
        if (insert) {
            //发送mq消息
            rabbitTemplate.convertAndSend(RabbitMqConstants.GOODS_EXCHANGE, "", goods);
        }
        return ResultEntity.success(insert);
    }

    @RequestMapping("/getGoodsPage")
    public Page<Goods> getGoodsPage(@RequestBody Page<Goods> page) {
        return goodsService.selectPage(page);

    }

    @RequestMapping("/getGoodsById/{id}")
    Goods getGoodsById(@PathVariable("id") Integer id) {
        EntityWrapper entityWrapper = new EntityWrapper();
        entityWrapper.eq("id", id);
        return goodsService.selectOne(entityWrapper);
    }

    public static void main(String[] args) {
//        int [] a= new int[5];
//        List list=new ArrayList();
//        List<int[]> ints = Arrays.asList(a);
//        System.out.println(ints.toString());
//        Object[] objects = list.toArray();
//        System.out.println(objects.toString());

    }



}
